<?php

 
 Zend_Loader::loadClass("BaseDBRows");
Zend_Loader::loadClass("InventoryDAA");
class InventoryDAAs extends BaseDBRows
{
	protected function setUp()
	{
		$this->_tableName = InventoryDAA::_tablename;
	}	
	
	public function getOrgInventoryDAAByInvId($invId="") 
	{
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("date_mortality, quantity_daa, if(length(reason)>50,concat(left(reason,50),'...'),reason) reason, inventory_id");
		$query->addSelect("currency_mark, stock_amount, inventory_daa_id");
		$query->addFrom($this->_tableName . " as id ");
		$query->addWhere("id.inventory_id = " . $invId);
		$query->addOrderBy("date_mortality desc");
		
		$db = DBCon::instance();
		// echo  $query->createSQL();die();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}
	
	public function getOrgDAAReportByOrgId($orgId="",$tank="",$moid="",$sizeCode="",$dateFrom="",$dateTo="")
	{
		Zend_Loader::loadClass("Inventory");
		Zend_Loader::loadClass("OrganizationOrganism");
		Zend_Loader::loadClass("Organism");
		Zend_Loader::loadClass("OrganismAttribute");
		Zend_Loader::loadClass("Size");
		Zend_Loader::loadClass("Tank");
		Zend_Loader::loadClass("QueryCreator");	
		
		$query = new QueryCreator();
		$query->addSelect("t.name tank_name, o.moid, o.name common_name, s.size, i.inventory_id, id.*");
		$query->addSelect("if(length(id.reason)>50,concat(left(id.reason,50),'...'),id.reason) remark ");
		$query->addFrom($this->_tableName . " id ");
		$query->addJoin(", " . Inventory::_tablename . " i ");
		$query->addJoin(", " . OrganizationOrganism::_tablename . " oo ");
		$query->addJoin(", " . Organism::_tablename . " o ");
		$query->addJoin(", " . OrganismAttribute::_tablename . " oa ");
		$query->addJoin(", " . Size::_tablename . " s ");
		$query->addJoin(", " . Tank::_tablename . " t ");
		$query->addWhere("id.inventory_id = i.inventory_id");
		$query->addWhere("i.org_organism_id = oo.org_organism_id");
		$query->addWhere("i.org_id = oo.org_id");
		$query->addWhere("oo.organism_id = o.organism_id");
		$query->addWhere("i.organism_attribute_id = oa.organism_attribute_id");
		$query->addWhere("oa.size_code = s.size_code");
		$query->addWhere("i.tank_id = t.tank_id");
		$query->addWhere("i.org_id = ". $orgId );
		$query->addOrderBy("t.name, o.moid, o.name, s.size, id.date_mortality desc");
		
		// tank
		if(strlen($tank) > 0) {
			$query->addWhere("t.tank_id = " . $tank);
		}
		// moid
		if(strlen($moid) > 0) {
			$query->addWhere("o.moid = '" . $moid . "'");	
		}
		// size code
		if(strlen($sizeCode) > 0) {
			$query->addWhere(" oa.size_code = '" . $sizeCode . "'");	
		}
		// dates from - to
		if(strlen($dateFrom) > 0 && strlen($dateTo) > 0) {
			$query->addWhere(" id.date_mortality between least('" . $dateFrom . "','" . $dateTo . "') and greatest('" . $dateFrom . "','" . $dateTo . "')" );	
		}
		elseif(strlen($dateFrom) > 0) {
			$query->addWhere(" id.date_mortality >= '" . $dateFrom . "'" );
		}
		elseif(strlen($dateTo) > 0) {
			$query->addWhere(" id.date_mortality <= '" . $dateTo . "'");
		}
		
		$db = DBCon::instance();
		//echo  $query->createSQL();die();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}
	
}
?>